﻿// -
// <copyright file="ClientLibraryUtils.cs" company="Microsoft Corporation">
//    Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// -

namespace Microsoft.Hawaii
{
    using System;
    using System.IO;
    using System.Security;

    /// <summary>
    /// A class to store utility functions used by the client libraries.
    /// </summary>
    public static class ClientLibraryUtils
    {
        /// <summary>
        /// Looks for a configuration file at the path specified.  If the file exists, it returns the hostname
        /// specified within the file.  If it does not exist, it returns the default.
        /// The [SecurityCritical] sttribute is needed due to CA2140 analysis rules.
        /// </summary>
        /// <param name="configFilePath">The config file path to look for.</param>
        /// <param name="defaultHostName">The default value to return if the config file does not exist.</param>
        /// <returns>The host name specified in the config file, if present.  The default otherwise.</returns>
        [SecuritySafeCritical]
        public static string LookupHostNameFromConfig(string configFilePath, string defaultHostName)
        {
            // Check to see if there is a config file on the system that specifies where the service staging server is.
            // Do not do this if running from the phone.
            #if !WINDOWS_PHONE && !SILVERLIGHT
            try
            {
                if (File.Exists(configFilePath))
                {
                    // The following try/finally block is needed to avoid CA2202 and CA2000 warnings.
                    // See http://msdn.microsoft.com/en-us/library/ms182334.aspx for more details.
                    string hostName;
                    FileStream configFileStream = null;
                    try
                    {
                        configFileStream = File.Open(configFilePath, FileMode.Open, FileAccess.Read);
                        using (TextReader reader = new StreamReader(configFileStream))
                        {
                            configFileStream = null;
                            hostName = reader.ReadLine();
                        }
                    }
                    finally
                    {
                        if (configFileStream != null)
                        {
                            configFileStream.Dispose();
                        }
                    }

                    // If we successfully read the host name from the config file, set it.
                    if (!string.IsNullOrEmpty(hostName))
                    {
                        return hostName;
                    }
                }
            }
            catch (IOException)
            {
                // If we hit an IO exception, we fall back to the default.
            }
            catch (UnauthorizedAccessException)
            {
                // If we hit an access exception when reading the config, use the default.
                // These catches are not expected to be hit, but mainly here to keep from
                // crashing if somebody outside the test team manages to create these exact
                // files but not have read permissions set.
            }
            #endif

            // Otherwise use the default.
            return defaultHostName;
        }
    }
}
